package com.waya.web.browser.webview;

import android.annotation.TargetApi;
import android.net.http.SslError;
import android.os.Build;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Created by ruby on 2018/1/12.
 */

public class WebDefaultViewClient extends WebViewClient {

  private String loadErrorUrl;

  @TargetApi(Build.VERSION_CODES.M) @Override
  public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
    super.onReceivedError(view, request, error);
    if (request.isForMainFrame()) {
      //高版本
      loadErrorPage(view);
    }
  }

  @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    // 接受所有网站的证书
   // handler.proceed();

  }

  @Override
  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    super.onReceivedError(view, errorCode, description, failingUrl);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      return;
    }
    loadErrorPage(view);
  }

  private void loadErrorPage(WebView view) {
    view.stopLoading();
    view.loadUrl(this.loadErrorUrl);
  }

  public void setLoadErrorUrl(String loadErrorUrl) {
    this.loadErrorUrl = loadErrorUrl;
  }
}
